package com.managedBean.user;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.controller.merchant.OrderController;
import com.dto.Item;
import com.dto.merchant.OrderDTO;
import com.dto.merchant.OrderItemsDTO;
import com.managedBean.cart.CartManagedBean;
import com.managedBean.registration.LoginManagedBean;
import com.utils.JsfUtils;

@ViewScoped
@ManagedBean(name = "myOrdersManagedBean")
public class MyOrdersManagedBean implements Serializable {

	private static final long serialVersionUID = -5333919842468069173L;
	@ManagedProperty(value = "#{orderController}")
	private OrderController orderController;
	private List<OrderDTO> orderList;
	LoginManagedBean loginBean;
	private OrderDTO selectedOrderDTO;
	private OrderItemsDTO selectedItem;
	@PostConstruct
	public void init() {
		 loginBean = (LoginManagedBean) JsfUtils
					.getBeanInstanceFromFacesContext("login");
		 selectedOrderDTO=new OrderDTO();
		orderList = orderController.findMyOrders(loginBean.getUserDTO().getIduserTable());
		if(orderList.size()>0){
			setSelectedOrderDTO(orderList.get(0));
		}
	}

	public void addToCart() {
		CartManagedBean cart = (CartManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("cart");
		Item item=new Item();
		item.setMoreSizeItemId(selectedItem.getOrderItemId());
		item.setQty(1);
		item.setPrice(selectedItem.getPrice());
		item.setBrand(selectedItem.getBrand());
		item.setItemImagePath(selectedItem.getOrderItemImage());
		item.setName(selectedItem.getOrderItemName());
		item.setSize(selectedItem.getSize());
		cart.addToCart(item);
	}
	
	/**
	 * @return the orderController
	 */
	public OrderController getOrderController() {
		return orderController;
	}

	/**
	 * @param orderController
	 *            the orderController to set
	 */
	public void setOrderController(OrderController orderController) {
		this.orderController = orderController;
	}

	/**
	 * @return the orderList
	 */
	public List<OrderDTO> getOrderList() {
		return orderList;
	}

	/**
	 * @param orderList the orderList to set
	 */
	public void setOrderList(List<OrderDTO> orderList) {
		this.orderList = orderList;
	}

	/**
	 * @return the loginBean
	 */
	public LoginManagedBean getLoginBean() {
		return loginBean;
	}

	/**
	 * @param loginBean the loginBean to set
	 */
	public void setLoginBean(LoginManagedBean loginBean) {
		this.loginBean = loginBean;
	}

	/**
	 * @return the selectedOrderDTO
	 */
	public OrderDTO getSelectedOrderDTO() {
		return selectedOrderDTO;
	}

	/**
	 * @param selectedOrderDTO the selectedOrderDTO to set
	 */
	public void setSelectedOrderDTO(OrderDTO selectedOrderDTO) {
		this.selectedOrderDTO = selectedOrderDTO;
	}

	/**
	 * @return the selectedItem
	 */
	public OrderItemsDTO getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(OrderItemsDTO selectedItem) {
		this.selectedItem = selectedItem;
	}

	
	

}
